﻿Imports System.Windows.Forms
Imports System.Drawing

Friend NotInheritable Class LayoutUtils
    ' Methods
    Private Sub New()
    End Sub

    Public Shared Function GetPreferredCheckBoxHeight(ByVal checkBox As CheckBox) As Integer
        Return LayoutUtils.GetPreferredHeight(checkBox, checkBox.UseCompatibleTextRendering, checkBox.Width)
    End Function

    Private Shared Function GetPreferredHeight(ByVal c As Control, ByVal useCompatibleTextRendering As Boolean, ByVal requiredWidth As Integer) As Integer
        Using graphics As Graphics = graphics.FromHwnd(c.Handle)
            If useCompatibleTextRendering Then
                Return graphics.MeasureString(c.Text, c.Font, c.Width).ToSize.Height
            End If
            Return TextRenderer.MeasureText(graphics, c.Text, c.Font, New Size(requiredWidth, &H7FFFFFFF), TextFormatFlags.WordBreak).Height
        End Using
    End Function

    Public Shared Function GetPreferredLabelHeight(ByVal label As Label) As Integer
        Return LayoutUtils.GetPreferredLabelHeight(label, label.Width)
    End Function

    Public Shared Function GetPreferredLabelHeight(ByVal label As Label, ByVal requiredWidth As Integer) As Integer
        Return LayoutUtils.GetPreferredHeight(label, label.UseCompatibleTextRendering, requiredWidth)
    End Function

    Public Shared Sub MirrorControl(ByVal c As Control)
        c.Left = (((c.Parent.Right - c.Parent.Padding.Left) - c.Margin.Left) - c.Width)
        If (((c.Anchor And AnchorStyles.Left) = AnchorStyles.None) OrElse ((c.Anchor And AnchorStyles.Right) = AnchorStyles.None)) Then
            c.Anchor = (c.Anchor And Not AnchorStyles.Left)
            c.Anchor = (c.Anchor Or AnchorStyles.Right)
        End If
    End Sub

    Public Shared Sub MirrorControl(ByVal c As Control, ByVal pivot As Control)
        c.Left = (pivot.Right - c.Width)
        If (((c.Anchor And AnchorStyles.Left) = AnchorStyles.None) OrElse ((c.Anchor And AnchorStyles.Right) = AnchorStyles.None)) Then
            c.Anchor = (c.Anchor And Not AnchorStyles.Left)
            c.Anchor = (c.Anchor Or AnchorStyles.Right)
        End If
    End Sub

    Public Shared Sub UnmirrorControl(ByVal c As Control)
        c.Left = ((c.Parent.Left + c.Parent.Padding.Left) + c.Margin.Left)
        If (((c.Anchor And AnchorStyles.Left) = AnchorStyles.None) OrElse ((c.Anchor And AnchorStyles.Right) = AnchorStyles.None)) Then
            c.Anchor = (c.Anchor And Not AnchorStyles.Right)
            c.Anchor = (c.Anchor Or AnchorStyles.Left)
        End If
    End Sub

    Public Shared Sub UnmirrorControl(ByVal c As Control, ByVal pivot As Control)
        c.Left = pivot.Left
        If (((c.Anchor And AnchorStyles.Left) = AnchorStyles.None) OrElse ((c.Anchor And AnchorStyles.Right) = AnchorStyles.None)) Then
            c.Anchor = (c.Anchor And Not AnchorStyles.Right)
            c.Anchor = (c.Anchor Or AnchorStyles.Left)
        End If
    End Sub

End Class


